package unitTest;

import baseObject.BrickBreaker;
import baseObject.IBrickBreaker;
import baseObject.IBrickBreaker.TFinished;
import baseObject.IBrickBreaker.TMove;
import junit.framework.TestCase;


public class BrickBreakerUnitTest extends TestCase{
	private IBrickBreaker brickBreaker;

	protected void setUp() {
		brickBreaker = new BrickBreaker();
	}

	public void testBrickBreaker() {
		assertTrue(this.brickBreaker.isFinished() == TFinished.NO);
		assertTrue(this.brickBreaker.getPlayerDirection() == TMove.NONE);
		assertTrue(this.brickBreaker.getBall() != null);
		assertTrue(this.brickBreaker.getPaddle() != null);
		assertTrue(this.brickBreaker.getWall() == null);
		
		this.brickBreaker.setPlayerDirection(TMove.LEFT);
		assertTrue(this.brickBreaker.getPlayerDirection() == TMove.LEFT);
			
		this.brickBreaker.initLevelOne();
		assertTrue(this.brickBreaker.getWall().getListBrick().size() > 0);
		
		int max = 0;
		while(max < 500){
			this.brickBreaker.moveBall();
			System.out.println("[MOVE BALL]");
			
			assertTrue(this.brickBreaker.getBall().getX() <= IBrickBreaker.XMAX);
			assertTrue(this.brickBreaker.getBall().getX() > 0);
			assertTrue(this.brickBreaker.getBall().getY() <= IBrickBreaker.YMAX);
			assertTrue(this.brickBreaker.getBall().getY() > 0);
			
			System.out.println("[MOVE PADDLE]");
			
			if(max < 10){
				this.brickBreaker.movePaddle(TMove.LEFT);
				System.out.println("LEFT");
			}else{
				this.brickBreaker.movePaddle(TMove.RIGHT);
				System.out.println("RIGHT");
			}
		
			
			assertTrue(this.brickBreaker.getPaddle().getX() <= IBrickBreaker.XMAX);
			assertTrue(this.brickBreaker.getPaddle().getX() > 0);
			assertTrue(this.brickBreaker.getPaddle().getY() > 0);
			
			if(this.brickBreaker.getPaddle().getX() == IBrickBreaker.XMAX){
				int k = 0;
				while(k < 20){
					this.brickBreaker.movePaddle(TMove.LEFT);
					this.brickBreaker.calculateStep();
					assertTrue(this.brickBreaker.getBall().getX() <= IBrickBreaker.XMAX);
					assertTrue(this.brickBreaker.getBall().getX() > 0);
					assertTrue(this.brickBreaker.getBall().getY() <= IBrickBreaker.YMAX);
					assertTrue(this.brickBreaker.getBall().getY() > 0);
					k++;
				}
			}
			
			this.brickBreaker.calculateStep();
			System.out.println("[CALCULATE STEP]");
			
			if(this.brickBreaker.isFinished() != TFinished.WIN){
				assertTrue(!this.brickBreaker.getWall().getListBrickBreakeable().isEmpty());
			}else{
				assertTrue(this.brickBreaker.getWall().getListBrickBreakeable().isEmpty());
			}
			System.out.println(this.getStateOfGame());
			max++;
		}
	}
	
	private String getStateOfGame(){
		StringBuilder sb = new StringBuilder();
		sb.append("[MOVE BALL POSITION : ]");
		sb.append(this.brickBreaker.getBall().getX());
		sb.append(" ");
		sb.append(this.brickBreaker.getBall().getY());
		sb.append("\n");
		sb.append("---------");
		sb.append("\n");
		sb.append("[POSITION PADDLE : ]");
		sb.append(this.brickBreaker.getPaddle().getX());
		sb.append(" ");
		sb.append(this.brickBreaker.getPaddle().getY());
		sb.append("\n");
		sb.append("---------");
		sb.append("\n");
		sb.append("GAME STATE");
		sb.append(" ");
		sb.append("[WALL]");
		sb.append(" ");
		sb.append("[BRICK]");
		sb.append(" ");
		sb.append("[ALL]");
		sb.append(this.brickBreaker.getWall().getListBrick().size());
		sb.append(" ");
		sb.append("[BREAKABLE]");
		sb.append(" ");
		sb.append(this.brickBreaker.getWall().getListBrickBreakeable().size());
		sb.append("\n");
		sb.append("---------");
		
		return sb.toString();
	}
}
